#include "StdAfx.h"
#include "LoginInfo.h"

CLoginInfo::CLoginInfo(void)
{
}

CLoginInfo::~CLoginInfo(void)
{
}

BOOL CLoginInfo::Init( LPCTSTR lpszInfo )
{
	//MessageBox(0,lpszInfo,_T(""),0);
	if (lpszInfo == NULL)
	{
		return FALSE;
	}
	CString strInfo(lpszInfo);
	int iLocation = 0;
	while ( TRUE )
	{
		iLocation  = strInfo.Find(_T(" /"),iLocation);
		if ( iLocation == -1 )
		{
			break;
		}
		iLocation += 2;
		int iValueBeginLocation = strInfo.Find(_T(":"),iLocation);
		if ( iValueBeginLocation == -1 )
		{
			break;
		}

		CString strInfoName = strInfo.Mid(iLocation,iValueBeginLocation - iLocation);

		iValueBeginLocation += 1;
		int iValueEndLocation = strInfo.Find(_T(" "),iValueBeginLocation);
		if (iValueEndLocation == -1)
		{
			iValueEndLocation = strInfo.GetLength();
		}
		CString strInfoValue = strInfo.Mid(iValueBeginLocation ,iValueEndLocation - iValueBeginLocation);
		if (strInfoName.GetLength() > 0 && strInfoValue.GetLength() > 0)
		{
			SetInfo(strInfoName,strInfoValue);
			//MessageBox(0,strInfoName,strInfoValue,0);
		}
	}
	return TRUE;
}

BOOL CLoginInfo::SetInfo( LPCTSTR lpszInfoName, LPCTSTR lpszInfoValue )
{
	if(NULL == lpszInfoName || NULL == lpszInfoValue)
	{
		return FALSE;
	}

	NameValuePair NameValue;
	NameValue.Name = lpszInfoName;
	NameValue.Value = lpszInfoValue;
	int iSize = m_aryNameValue.GetSize() - 1;
	while(iSize >= 0)
	{
		if(0 == m_aryNameValue[iSize].Name.CompareNoCase(NameValue.Name))
		{
			m_aryNameValue[iSize] = NameValue;
			return TRUE;
		}
		iSize--;
	}
	if(iSize < 0)
		return m_aryNameValue.Add(NameValue);

	return FALSE;
}

LPCTSTR CLoginInfo::GetInfo( LPCTSTR lpszInfoName )
{
	int iSize = m_aryNameValue.GetSize() - 1;
	while(iSize >= 0)
	{
		if(0 == m_aryNameValue[iSize].Name.CompareNoCase(lpszInfoName))
		{
			return m_aryNameValue[iSize].Value;
		}
		iSize--;
	}
	return NULL;
}

int CLoginInfo::GetInfo( LPCTSTR lpszInfoName, int iDefault )
{
	int iSize = m_aryNameValue.GetSize() - 1;
	while(iSize >= 0)
	{
		if(0 == m_aryNameValue[iSize].Name.CompareNoCase(lpszInfoName))
		{
			iDefault = _ttoi(m_aryNameValue[iSize].Value);
			break;
		}
		iSize--;
	}
	return iDefault;
}

int CLoginInfo::GetSubInfoCount( LPCTSTR lpszInfoName )
{
	int iCount = 0;
	CString strInfoValue = GetInfo(lpszInfoName);
	if (!strInfoValue.IsEmpty())
	{
		iCount ++;
		int iLocation = 0;
		while( TRUE )
		{
			iLocation = strInfoValue.Find(_T("|"),iLocation);
			if ( iLocation == -1 )
			{
				break;
			}
			else
			{
				iLocation ++;
				iCount ++;
			}
		}
	}
	return iCount;
}

CString CLoginInfo::GetSubInfo( LPCTSTR lpszInfoName, int iIndex )
{
	CString strInfoValue = GetInfo(lpszInfoName);
	if (!strInfoValue.IsEmpty())
	{
		int iCurrentIndex = 0;
		int iLocation = 0;
		int iBegin = 0;
		while( TRUE )
		{
			iBegin = iLocation;
			iLocation = strInfoValue.Find(_T("|"),iLocation);
			if ( iIndex == iCurrentIndex )
			{
				int iValueSize = 0;
				if ( iLocation == -1 )
				{
					iValueSize = strInfoValue.GetLength() - iBegin;
				}
				else
				{
					iValueSize = iLocation - iBegin;
				}
				return strInfoValue.Mid(iBegin,iValueSize);
			}
			if ( iLocation == -1 )
			{
				break;
			}
			else
			{
				iLocation ++;
				iCurrentIndex ++;
			}
		}
	}
	return _T("");
}

int CLoginInfo::GetChannelInfoCount()
{
	int iCount = GetSubInfoCount(CHANNEL_INFO_NAME);
	if (iCount > 0)
	{
		iCount = iCount/4;
	}
	return iCount;
}

BOOL CLoginInfo::GetChannelInfoByIndex( int iIndex ,LoginChannelInfo* pLoginInfo )
{
	if ( pLoginInfo == NULL || GetChannelInfoCount() < (iIndex + 1))
	{
		return FALSE;
	}
	else
	{
		CString strValue;
		strValue = GetSubInfo(CHANNEL_INFO_NAME,iIndex*4);
		pLoginInfo->type = _ttoi(strValue);
		strValue = GetSubInfo(CHANNEL_INFO_NAME,iIndex*4 + 1);
		pLoginInfo->cid = _ttoi(strValue);
		strValue = GetSubInfo(CHANNEL_INFO_NAME,iIndex*4 + 2);
		pLoginInfo->did = _ttoi(strValue);
		strValue = GetSubInfo(CHANNEL_INFO_NAME,iIndex*4 + 3);
		pLoginInfo->gid = _ttoi(strValue);
	}
	return TRUE;
}

BOOL CLoginInfo::GetChannelInfoByType( int iType ,LoginChannelInfo* pLoginInfo )
{
	int iCount = GetChannelInfoCount();
	for (int i = 0 ; i < iCount ; i++)
	{
		if ( iType == _ttoi(GetSubInfo(CHANNEL_INFO_NAME,i*4)))
		{
			return GetChannelInfoByIndex(i,pLoginInfo);
		}
	}
	return FALSE;
}